package com.whuan.student.util;

import com.whuan.student.dataObject.Student;
import com.whuan.student.repository.StudentRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import sun.applet.Main;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.Random;
@Slf4j
public class IDUtils {

    /**
     * 生成随机图片名
     */
    public static String genImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        //如果不足三位前面补0
        String str = millis + String.format("%03d", end3);

        return str;
    }
    /**
     * 生成 11 位 studentId
     */
    public static String getStudentId() {

        SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式 yyyyMMdd HHmmss
        String userId = df.format(new Date())+(int)(Math.random()*900+100);
        String str = "st"+userId;
/*        Optional<Student> Student = studentRepository.findById(str);

        if(Student.isPresent()) {
            str = getStudentId();

        }*/
        return str;
    }
    /**
     * 生成 11 位 studentId
     */
    public static String getBatchStudentId(Integer index) {

        SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssSSS");//设置日期格式 yyyyMMdd HHmmss
        Integer userid = 101 + index;
        String str = "bs"+df.format(new Date()) + userid;
/*        Optional<Student> Student = studentRepository.findById(str);

        if(Student.isPresent()) {
            str = getStudentId();

        }*/
        return str;
    }
    public static void main(String[] args){
        String studentId = getBatchStudentId(6);
        log.info("studentId={}",studentId);
    }
}
